package com.lemon.smartsafetynew.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.SharedPreferencesHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafetynew.entity.common.CommonBo;
import com.lemon.smartsafetynew.ui.activity.user.UserLoginActivity;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.service.XGPushService;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Cooliean on 16/1/15.
 */
public class SplashActivity extends Activity {

    private final int SPLASH_DISPLAY_LENGTH = 3000; // 延迟


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        //add by Andrew.yin 2017/6/29 begin. 修复安装新应用时，选择“打开”和“完成”产生的问题。
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        //add by Andrew.yin 2017/6/29 end.

        LogUtils.LOGE("getPackageName:", getPackageName());
        initForMYOP();
        //startActivity(new Intent(this, LemonBaiduActivity.class));

        new Handler().postDelayed(new Runnable() {
            public void run() {
                //changed by Andrew.yin 2017/7/17 begin
                Intent mainIntent;
                if (AccountUtil.isLogin()) {
                    mainIntent = new Intent(SplashActivity.this, MainNewActivity.class);
                } else {
                    mainIntent = new Intent(SplashActivity.this, UserLoginActivity.class);
                }
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
                //changed by Andrew.yin 2017/7/17 end
            }
        }, SPLASH_DISPLAY_LENGTH);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    private static Boolean isExit = false;
    private static Boolean hasTask = false;
    private Timer tExit = new Timer();
    private TimerTask task = new TimerTask() {
        @Override

        public void run() {
            isExit = false;
            hasTask = false;
        }
    };


    /**
     * 初始化各种开放平台
     */
    private void initForMYOP() {

        String strUserID = "";
        try {
            strUserID = new SharedPreferencesHelper(IApplication.context()).getUserId();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (!CommonUtil.strIsEmpty(strUserID)) {
            LoggerHelper.error(getClass(), "strUserID;    " + strUserID);
            XGPushManager.registerPush(getApplicationContext(), strUserID, new XGIOperateCallback() {
                @Override
                public void onSuccess(Object o, int i) {
                    Log.e("TPush", "注册成功，设备token为：" + o);
                }

                @Override
                public void onFail(Object o, int errCode, String msg) {
                    Log.e("TPush", "注册失败，错误码：" + errCode + ",错误信息：" + msg);
                }
            });

            // 2.36（不包括）之前的版本需要调用以下2行代码
            Intent service = new Intent(getApplicationContext(), XGPushService.class);
            getApplicationContext().startService(service);
        }

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isExit == false) {
                isExit = true;
                //showToast("再按一次退出程序");
                if (task != null) {
                    task.cancel();  //将原任务从队列中移除
                }
                task = new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false;
                        hasTask = false;
                    }
                };
                if (!hasTask) {
                    tExit.schedule(task, 2000);
                }
            } else {
                finish();
            }
        }
        return false;

    }
}
